/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Chart;


import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import me.aviator.entity.Ribbon;

/**
 *
 * @author Administrator
 */
public class ChartFactory {
    public static PieChart createPieChart(String chartname,List<String> lable,List<Double> dataset,Ribbon ribbon,Rectangle2D area){
        PieChart pieCr=new PieChart(chartname,lable,dataset,ribbon);
        ArrayList<String> piefract=new ArrayList<>();
        double centerX=area.getCenterX();
        double centerY=area.getCenterY();
        double radius=area.getWidth()/2;
        double dataSum=0;
        double angleSum1=0;
        double angleSum2=0;
        
        for(Double v:dataset){
            dataSum+=Math.abs(v);
        }
        for(int i=0;i<dataset.size();i++){
            double angle=360*Math.abs(dataset.get(i))/dataSum;
            angleSum2=angleSum1+angle;
            double offsetX1=radius*Math.cos(angleSum1*Math.PI/180);
            double offsetY1=radius*Math.sin(angleSum1*Math.PI/180);
            double offsetX2=radius*Math.cos(angleSum2*Math.PI/180);
            double offsetY2=radius*Math.sin(angleSum2*Math.PI/180);
            
            double currentX=centerX+offsetX2;
            double currentY=centerY-offsetY2;
            
            // the vertical offset must be subtracted,
            // so we need to "flip" the sign of offsetY1
            offsetY1*=-1;
            String pointPath="";
            pointPath="M"+String.format("%f",centerX)+","+String.format("%f", centerY);
            pointPath+=" l"+String.format("%f", offsetX1)+","+String.format("%f", offsetY1);
            if(angle<180){
                pointPath+=" A"+String.format("%f",radius)+","+String.format("%f",radius)+" 0 0 0 ";
            }
            else{
                pointPath+=" A"+String.format("%f",radius)+","+String.format("%f",radius)+" 0 1 0 ";
            }
            pointPath+=String.format("%f",currentX)+","+String.format("%f",currentY);
            pointPath+=" L"+String.format("%f",centerX)+","+String.format("%f",centerY)+"z";
            
            piefract.add(pointPath);
            angleSum1+=angle;
        }
        pieCr.setpiefract(piefract);
        return pieCr;
    }
    
    public static PieChart3D createPieChart3D(String chartname,List<String> lable,List<Double> dataset,Ribbon ribbon,Rectangle2D area){
        PieChart3D pieCr=new PieChart3D(chartname,lable,dataset,ribbon);
        ArrayList<String> piefract=new ArrayList<>();
        double centerX=area.getCenterX();
        double centerY=area.getCenterY();
        double lRadius=area.getWidth()/2;
        double sRadius=lRadius*9/16;
        double height=lRadius/10;
        
        double dataSum=0;
        double angleSum1=0;
        double angleSum2=0;
        
        for(Double v:dataset){
            dataSum+=Math.abs(v);
        }
        for(int i=0;i<dataset.size();i++){
            double angle=360*Math.abs(dataset.get(i))/dataSum;
            angleSum2=angleSum1+angle;
            double offsetX1=lRadius*Math.cos(angleSum1*Math.PI/180);
            double offsetY1=sRadius*Math.sin(angleSum1*Math.PI/180);
            double offsetX2=lRadius*Math.cos(angleSum2*Math.PI/180);
            double offsetY2=sRadius*Math.sin(angleSum2*Math.PI/180);
            
            double currentX=centerX+offsetX2;
            double currentY=centerY-offsetY2;
            
            // the vertical offset must be subtracted,
            // so we need to "flip" the sign of offsetY1
            offsetY1*=-1;
            String pointPath="";
            pointPath="M"+String.format("%f",centerX)+","+String.format("%f", centerY);
            pointPath+=" l"+String.format("%f", offsetX1)+","+String.format("%f", offsetY1);
            if(angle<180){
                pointPath+=" A"+String.format("%f",lRadius)+","+String.format("%f",sRadius)+" 0 0 0 ";
            }
            else{
                pointPath+=" A"+String.format("%f",lRadius)+","+String.format("%f",sRadius)+" 0 1 0 ";
            }
            pointPath+=String.format("%f",currentX)+","+String.format("%f",currentY);
            pointPath+=" L"+String.format("%f",centerX)+","+String.format("%f",centerY)+"z";
            
            piefract.add(pointPath);
            angleSum1+=angle;
        }
        String path="";
        path="M"+String.format("%f",centerX-lRadius)+","+String.format("%f", centerY);
        path+=" l"+String.format("%f", 0.0)+","+String.format("%f", height);
        path+=" A"+String.format("%f",lRadius)+","+String.format("%f",sRadius)+" 0 1 0 ";
        path+=String.format("%f",centerX+lRadius)+","+String.format("%f",centerY+height);
        path+=" l"+String.format("%f", 0.0)+","+String.format("%f", -height);
        path+="M"+String.format("%f",centerX-lRadius)+","+String.format("%f", centerY);
        path+=" A"+String.format("%f",lRadius)+","+String.format("%f",sRadius)+" 0 1 0 ";
        path+=String.format("%f",centerX+lRadius)+","+String.format("%f",centerY)+"z";
        piefract.add(path);
        pieCr.setpiefract(piefract);
        
        return pieCr;
    }
    
    public static BarChart createBarChart(String chartname,List<String> lable,List<Double> dataset,Ribbon ribbon,double x,double y,double step){
        BarChart barCr=new BarChart(chartname,lable,dataset,ribbon);
        ArrayList<String> barfract=new ArrayList<>();
        double originX=x;
        double originY=y;
        double maxval=0;
        for(double m:dataset){
            if(m>maxval)
            {
                maxval=m;
            }
        }
        double offsetX=(dataset.size()+2)*step;
        double offsetY=-(Math.floor(maxval/5000)+1)*2950;
        String Xaxis="M"+originX+","+originY;
        Xaxis+="l"+offsetX+","+0;
        barCr.setXaxis(Xaxis);
        String Yaxis="M"+originX+","+originY;
        Yaxis+="l"+0+","+offsetY;
        barCr.setYaxis(Yaxis);
        
        for(int i=0;i<dataset.size();i++){
            double x1=originX+(i+1)*step;
            offsetY=-(dataset.get(i)/5000)*2950;
            String path="M"+x1+","+originY;
            path+="l"+0+","+offsetY;
            offsetY=-offsetY;
            path+="l"+step+","+0;
            path+="l"+0+","+offsetY;
            double step1=-step;
            path+="l"+step1+","+0+"z";
            barfract.add(path);
        }
        barCr.setBarfract(barfract);
        return barCr;
    }
}
